using System;
using System.Collections;
using System.Xml.Serialization;
using Microsoft.SharePoint;

namespace Core.ListViewObjectModel
{
	[System.Xml.Serialization.XmlRootAttribute(Namespace="FlexListViewer.ListViewerObjectModel", IsNullable=false)]
	public class ListView
	{
		#region Private members
		private string _viewId = string.Empty;
		private string _listId = string.Empty;
		private string _displayName = string.Empty;
		private string _siteUrl = string.Empty;
		//		private string _siteRelativeUrl = string.Empty;
		//		private string _serverRelativeUrl = string.Empty;
		private string _absoluteUrl = string.Empty;
		private string _rootUrl = string.Empty;
		private string _displayFormUrl = string.Empty;
		private string _newFormUrl = string.Empty;
		private string _editFormUrl = string.Empty;
		private SPBaseType _baseType = SPBaseType.UnspecifiedBaseType;
		private int _rowLimit = 0;
		private ListViewFieldCollection _fields = null;
		private ListViewRowCollection _rows = null;
		#endregion

		#region Public properties
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string ViewID
		{
			set
			{
				_viewId = value;
			}
			get
			{
				return _viewId;
			}
		}
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string ListID
		{
			set
			{
				_listId = value;
			}
			get
			{
				return _listId;
			}
		}
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string DisplayName
		{
			set
			{
				_displayName = value;
			}
			get
			{
				return _displayName;
			}
		}
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string SiteUrl
		{
			set
			{
				_siteUrl = value;
			}
			get
			{
				return _siteUrl;
			}
		}
		//		[System.Xml.Serialization.XmlAttributeAttribute()]
		//		public string SiteRelativeUrl
		//		{
		//			set
		//			{
		//				_siteRelativeUrl = value;
		//			}
		//			get
		//			{
		//				return _siteRelativeUrl;
		//			}
		//		}
		//		[System.Xml.Serialization.XmlAttributeAttribute()]
		//		public string ServerRelativeUrl
		//		{
		//			set
		//			{
		//				_serverRelativeUrl = value;
		//			}
		//			get
		//			{
		//				return _serverRelativeUrl;
		//			}
		//		}
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string AbsoluteUrl
		{
			set
			{
				_absoluteUrl = value;
			}
			get
			{
				return _absoluteUrl;
			}
		}

		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string RootUrl
		{
			set
			{
				_rootUrl = value;
			}
			get
			{
				return _rootUrl;
			}
		}

		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string DisplayFormUrl
		{
			set
			{
				_displayFormUrl = value;
			}
			get
			{
				return _displayFormUrl;
			}
		}

		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string NewFormUrl
		{
			set
			{
				_newFormUrl = value;
			}
			get
			{
				return _newFormUrl;
			}
		}

		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string EditFormUrl
		{
			set
			{
				_editFormUrl = value;
			}
			get
			{
				return _editFormUrl;
			}
		}

		[System.Xml.Serialization.XmlAttributeAttribute()]
		public SPBaseType BaseType
		{
			set
			{
				_baseType = value;
			}
			get
			{
				return _baseType;
			}
		}

		[System.Xml.Serialization.XmlAttributeAttribute()]
		public int RowLimit
		{
			set
			{
				_rowLimit = value;
			}
			get
			{
				return _rowLimit;
			}
		}

		public ListViewFieldCollection Fields
		{
			set
			{
				_fields = value;
			}
			get
			{
				return _fields;
			}
		}

		//		[XmlElement()] // Skip next level (dunno why...)
		public ListViewRowCollection Rows
		{
			set
			{
				_rows = value;
			}
			get
			{
				return _rows;
			}
		}
		#endregion

		#region Constructor(s)
		public ListView()
		{
			_fields = new ListViewFieldCollection();
			_rows = new ListViewRowCollection();
		}
		#endregion
	}
	public class ListViewField
	{
		#region Private members
		private string _name = string.Empty;
		private string _displayName = string.Empty;
		private string _fieldType = string.Empty;
		private bool _hidden = false;
		private bool _linkToItem = false;
		#endregion
		#region Public properties
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string Name
		{
			set
			{
				_name = value;
			}
			get
			{
				return _name;
			}
		}
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string DisplayName
		{
			set
			{
				_displayName = value;
			}
			get
			{
				return _displayName;
			}
		}
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string FieldType
		{
			set
			{
				_fieldType = value;
			}
			get
			{
				return _fieldType;
			}
		}
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public bool Hidden
		{
			set
			{
				_hidden = value;
			}
			get
			{
				return _hidden;
			}
		}
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public bool LinkToItem
		{
			set
			{
				_linkToItem = value;
			}
			get
			{
				return _linkToItem;
			}
		}
		#endregion
	}

	public class ListViewFieldCollection : ArrayList
	{
		public int Add(ListViewField value)
		{
			return base.Add (value);
		}

		public override int Add(object value)
		{
			throw (new NotImplementedException());
		}

		public ListViewField this[int index]
		{
			get
			{
				return (ListViewField)base[index];
			}
			set
			{
				base[index] = value;
			}
		}
	}

	public class ListViewRowCollection : ArrayList
	{
		public int Add(ListViewRow value)
		{
			return base.Add (value);
		}

		public override int Add(object value)
		{
			throw (new NotImplementedException());
		}

		public ListViewRow this[int index]
		{
			get
			{
				return (ListViewRow)base[index];
			}
			set
			{
				base[index] = value;
			}
		}
	}

	public class ListViewRow
	{
		#region Private members
		private ListViewRowValueCollection _rowValues = null;
		private int _id = -1;
		#endregion

		#region Public properties
		//		[XmlArray("ListViewRow")] // Serialize under different name
		public ListViewRowValueCollection RowValues
		{
			set
			{
				_rowValues = value;
			}
			get
			{
				return _rowValues;
			}
		}
		/// <summary>
		/// Id of the item.
		/// </summary>
		[XmlAttribute()]
		public int Id
		{
			set
			{
				_id = value;
			}
			get
			{
				return _id;
			}
		}
		#endregion

		#region Constructor(s)
		public ListViewRow()
		{
			_rowValues = new ListViewRowValueCollection();
		}
		#endregion
	}
	public class ListViewRowValueCollection : ArrayList
	{
		public int Add(ListViewRowValue value)
		{
			return base.Add (value);
		}

		public override int Add(object value)
		{
			throw (new NotImplementedException());
		}

		public ListViewRowValue this[int index]
		{
			get
			{
				return (ListViewRowValue)base[index];
			}
			set
			{
				base[index] = value;
			}
		}
	}
	public class ListViewRowValue
	{
		#region Private members
		private string _fieldName = string.Empty;
		private ListViewRowValuePartCollection _parts;
		#endregion
		#region Public properties
		[System.Xml.Serialization.XmlAttributeAttribute()]
		public string FieldName
		{
			set
			{
				_fieldName = value;
			}
			get
			{
				return _fieldName;
			}
		}
		public ListViewRowValuePartCollection Parts
		{
			set
			{
				_parts = value;
			}
			get
			{
				return _parts;
			}
		}
		#endregion
		#region Constructor(s)
		public ListViewRowValue()
		{
			_parts = new ListViewRowValuePartCollection();
		}
		public ListViewRowValue(string fieldName)
		{
			_fieldName = fieldName;
			_parts = new ListViewRowValuePartCollection();
		}
		public ListViewRowValue(string fieldName, string singleValue)
		{
			_fieldName = fieldName;
			_parts = new ListViewRowValuePartCollection();
			_parts.Add(new ListViewRowValuePart(singleValue));
		}
		#endregion
	}

	public class ListViewRowValuePartCollection : ArrayList
	{
		public int Add(ListViewRowValuePart value)
		{
			return base.Add (value);
		}

		public override int Add(object value)
		{
			throw (new NotImplementedException());
		}

		public ListViewRowValuePart this[int index]
		{
			get
			{
				return (ListViewRowValuePart)base[index];
			}
			set
			{
				base[index] = value;
			}
		}
	}
	public class ListViewRowValuePart
	{
		#region Private members
		private string _value = string.Empty;
		#endregion
		#region Public properties
		//		[System.Xml.Serialization.XmlAttributeAttribute()]
		[XmlText()]
		public string Value
		{
			set
			{
				_value = value;
			}
			get
			{
				return _value;
			}
		}
		#endregion
		#region Constructor(s)
		public ListViewRowValuePart()
		{
		}
		public ListViewRowValuePart(string partValue)
		{
			_value = partValue;
		}
		#endregion
	}
}
